/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.readFromFile;

import cn.ac.iie.di.wx.vo.PicUrlVo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class ReadFromFile implements Runnable {

    public String filePath;
    public String fileName;
    public LinkedBlockingQueue<PicUrlVo> picUrlBlockingQueue;
    public static Logger logger = Logger.getLogger(ReadFromFile.class.getName());

    public ReadFromFile(String filePath, String fileName, LinkedBlockingQueue<PicUrlVo> picUrlBlockingQueue) {
        this.filePath = filePath;
        this.fileName = fileName;
        this.picUrlBlockingQueue = picUrlBlockingQueue;
    }

    @Override
    public void run() {
        //创建指定路径
        while (true) {
            File f = new File(filePath);
            if (!f.exists() || !f.isDirectory()) {
                logger.info("file path does not exist");
            }
            //创建指定
//            logger.info(filePath + fileName);
            File file = new File(filePath + fileName);
            if (!file.exists()) {
                try {
                    logger.info("this file doex not exist");
                    file.createNewFile();
                    Thread.sleep(1000);
                } catch (IOException ex) {
                    logger.error(ex.getMessage(), ex);
                } catch (InterruptedException ex) {
                    logger.error(ex.getMessage(), ex);
                }
            }

            if (file.canWrite() && file.canRead()) {
//                logger.info("file can ");
                //若有数据则进行读写文件

                FileOutputStream outputStream = null;
                try {
                    InputStream responseIn = new FileInputStream(file);
//                    logger.info(responseIn.available());
                    Reader reader = new InputStreamReader(responseIn);
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String text = "";
                    StringBuffer stringBuffer = new StringBuffer();
                    while ((text = bufferedReader.readLine()) != null) {
                        stringBuffer.append(text + "\r\n");
                        logger.info(text);
                    }

                    responseIn.close();
                    reader.close();
                    bufferedReader.close();
                    //文件内没有数据
                    if (stringBuffer.toString().equals("")) {
//                        logger.info("there is no data in the file");
                    } else {
                        String list = stringBuffer.toString();
                        String[] listArray = list.split("\r\n");
//                        logger.info(listArray.length);
                        for (String listArray1 : listArray) {
                            String[] listAr = listArray1.split(";");
                            picUrlBlockingQueue.put(new PicUrlVo(listAr[1], listAr[0]));
                        }
                        list = null;
                    }
                    //清空文件内容
                    String fileList = "";
                    outputStream = new FileOutputStream(file);
                    Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
                    writer.write(fileList);
                    writer.flush();
                    writer.close();
                    outputStream.close();
                    Thread.sleep(200);
                } catch (FileNotFoundException ex) {
                    logger.info(ex.getMessage(), ex);
                } catch (IOException ex) {
                    logger.info(ex.getMessage(), ex);
                } catch (InterruptedException ex) {
                    logger.info(ex.getMessage(), ex);
                } finally {

                }

            }
            f = null;
            file = null;
        }
    }

    public static void main(String[] args) {
        ReadFromFile readFromFile = new ReadFromFile("/home/jr/path/url/", "text.txt", new LinkedBlockingQueue<PicUrlVo>());
        Thread readFromFileThread = new Thread(readFromFile);
        readFromFileThread.start();
    }
}
